using System.Text;
using _ReplaceString_.UI;
using RSCore;
using Terraria.GameContent;
using Terraria.GameContent.UI.Elements;

namespace _ReplaceString_.UI.Elements;

public class PackageElement : UIBase
{
	private PackageID _id;

	private LeftPanel _leftPanel;

	public PackageElement(PackageID id, bool enable, LeftPanel leftPanel)
	{
		_id = id;
		ForegroundColor = enable ? Color.Green : Color.White;

		Height = new(0, 1);
		var text = new UIText(id.Name)
		{
			Width = new(0, 1),
			Height = new(0, 1),
			TextOriginX = 0.5f,
			TextOriginY = 0.5f,
			TextColor = ForegroundColor,
		};
		Append(text);
		OnUpdate += delegate
		{
			text.TextColor = ForegroundColor;
		};

		OnMouseOver += ShowInfo;
		_leftPanel = leftPanel;
	}

	private void ShowInfo(UIMouseEvent evt, UIElement listeningElement)
	{
		var build = _id.LoadPackage();
		var info = new StringBuilder();
		info.AppendLine(ModHelper.GetText("Config.Package", build.Name, build.Version));
		info.AppendLine(ModHelper.GetText("Config.Author", build.Author));
		info.AppendLine(ModHelper.GetText("Config.Mod", build.ModName, build.ModVersion));
		info.AppendLine(build.Description);
		_leftPanel.ShowInfo(info.ToString());
	}
}